?><?
class cls_control_js extends cls_control{
	var $action = "js";
	var $fname	= "grape_scripts.js";

	function check_access(){return true;}

	function as_page(){
		//
		$js="";
		$new_change_hash=0;
		//
		$change_hash=$this->getSet("package_scripts_hash");
		$predefined=array(
			"grape/scripts/grape.js",
		);
		$registered=array_merge($predefined,$GLOBALS["package_scripts"]);
		foreach($registered as $style){$new_change_hash+=filemtime($style);}
		$change_hash+=cls_script::changed_checksum();
		$this->setSet("package_scripts_hash",$new_change_hash);
		$change_hash=$new_change_hash;
		/// package and predefined styles
		foreach($registered as $script){
			$js.=file_get_contents($script);
		}
		/// user defined styles
		foreach(cls_script::get() as $script){
			$js.=$script->content;
		}
		file_put_contents($this->fname,$js);
		jr("ok");
	}


	function as_link(){
		?><<??>script src="<?=get_host()."/".$this->fname?>?<?=filemtime($this->fname)?>" type="text/javascript" ></script><?
	}
}